: iFlag@ ( xt -- f)
    \ return immediate status of the execution token.
    \ 0=non-immediate $8000=immediate.
    >link 2+ @ $8000 and ;

: iFlag! ( xt f -- )
    \ if f is true set the immediate status of the execution token
    \ to true, otherwise set it to false.
    swap >link 2+ dup >r @ swap if $8000 or else $7FFF and then r> ! ;
    
: setImmed ( xt1 xt2 -- )
    \ set xt2's immediate flag equal to xt1's immediate flag.
    swap iFlag@ iFlag! ;
    
: dnop ( -- )
    \ default run-time for newly created deferred words.
    true abort" Deferred action not set" ;
    
: defer ( "name" -- )
    \ create a deferred word from the name passed in on the command line.
    \ eg. defer this (creates a deferred word called this).
    create ['] dnop , does> @ execute ;
    
: is ( xt "name" -- ) 
    \ associate the deferred word "name" with the executable code addressed
    \ by xt. E.g: ' WORDS IS VLIST
    dup 0= abort" Target word not found"
    bl word find 0= abort" Deferred word not found" ( txt dxt )
    2dup >body !  setImmed ;
    
: defer@ ( xt1 -- xt2)
    \ given the execution token of deferred word xt1, return the execution
    \ token of the run-time code associated with it.
    \ e.g: DEFER VLIST  ' WORDS IS VLIST  ' VLIST DEFER@ $.
    \ (displays the execution token of WORDS)
    >body @ ;
    
: defer! ( xt1 xt2 -- )
    \ given the execution token of deferred word xt2, sets it such that
    \ executing xt2 executes the code addressed by xt1.
    2dup >body !  setImmed ;


\ Tests:
\ ------
\ standard deferred word (non-immediate)
defer cls
' page is cls
cls \ should clear the screen

\ immediate deferred word (the deferred word inherits the
\ immediate status of the target word)
defer print"  ' ." is print"
: test print" This is a test" cr ;
test

\ create a deferred word
defer thing

\ assign it to a non-immediate word via DEFER!
' mem  ' thing  defer!

\ thing should execute mem
thing

\ thing's immediate status should be 0:
' thing iFlag@ .

\ Now re-assign thing to an immediate word:
' ." ' thing defer!

\ create a definition using thing
: thing-test thing this is a test of thing" ;
thing-test

\ things immediate status should now be 8000 hex
\ since it now reflects an immediate word.
' thing iFlag@ $.

